package cn.zyq.ykb.controller.mgt.system;


import cn.zyq.ykb.beans.sys.SysPost;
import cn.zyq.ykb.service.sys.SysPostService;
import cn.zyq.ykb.system.annotation.Log;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.exception.BusinessException;
import cn.zyq.ykb.system.ry.enums.BusinessType;
import cn.zyq.ykb.system.ry.util.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/mgt/system/post")
public class SysPostController extends BaseController {
    @Autowired
    private SysPostService postService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @PostMapping("/list")
    public Object list(@RequestBody PageParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SysPost> list = postService.selectPostList(param);
        return new PageInfo<>(list);
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PageParam param) {
        List<SysPost> list = postService.selectPostList(param);
        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public Object getInfo(@PathVariable Integer postId) {
        return postService.selectPostById(postId);
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new BusinessException("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            throw new BusinessException("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return postService.insertPost(post);
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Object edit(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new BusinessException("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (!postService.checkPostCodeUnique(post)) {
            throw new BusinessException("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return postService.updatePost(post);
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public Object remove(@PathVariable Integer[] postIds) {
        return postService.deletePostByIds(postIds);
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public Object optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return posts;
    }
}
